import java.util.regex.Pattern;

public class db_statement_delete_from implements db_interface_statement {

	public String[] oTokens = new String[11];
	public Pattern[] oRegex = new Pattern[4];
	private db_util_parser oUtilParser = null;
	private db_parse_result_delete_from oRes = null;
	private int iTypeIndicator = 0;
	
	db_statement_delete_from() {
		oTokens[0] = "DELETE";
		oTokens[1] = "FROM";
		oTokens[2] = "WHERE";
		oTokens[3] = "NOT";
		oTokens[4] = "OR";
		oTokens[5] = "AND";
		oTokens[6] = "=";
		oTokens[7] = ">";
		oTokens[8] = "<";
		oTokens[9] = "<=";
		oTokens[10] = ">=";

		oRegex[0] = Pattern.compile("[a-zA-Z][0-9a-zA-Z]*");
		oRegex[1] = Pattern.compile("(-)?(\\d){1,9}");
		oRegex[2] = Pattern.compile("'([^']|'')*'");
		oRegex[3] = Pattern.compile("[a-zA-Z][0-9a-zA-Z]*");

		oUtilParser = new db_util_parser(oTokens);
	}
	public db_interface_parse_result parse(String sSQL) {

		oRes = new db_parse_result_delete_from();
		oUtilParser.restart(sSQL);
		int iRes = query();

		if (oUtilParser.sQuery.trim().length() > 0 && iRes == 0)
			iRes = -1;
		
		oRes.set_result(iRes);
		return oRes;
	}

	public int query() {
		if (!oUtilParser.matchReplace(oUtilParser.sQuery, oTokens[0]))
			return -2;
		if (!oUtilParser.matchReplace(oUtilParser.sQuery, oTokens[1]))
			return -2;
		if(tablelist() == -1)
			return -1;
		if (oUtilParser.matchReplace(oUtilParser.sQuery, oTokens[2])){
			return whereclause();
		}
		return 0;
	}

	public int tablelist() {

		if (!oUtilParser.matchReplace(oUtilParser.sQuery, oRegex[0]))
			return -1;
		oRes.sTableName = oUtilParser.sValue;
		return 0;
	}

	public int whereclause() {
		oUtilParser.matchReplace(oUtilParser.sQuery, oTokens[4]);
		
		if(comparison() == -1)
			return -1;

		if (oUtilParser.matchReplace(oUtilParser.sQuery, oTokens[4])){
			if (oUtilParser.matchReplace(oUtilParser.sQuery, oTokens[5]) || oUtilParser.matchReplace(oUtilParser.sQuery, oTokens[6])) {
				return whereclause();
			} else
				return -1;
		} else {
			if (oUtilParser.matchReplace(oUtilParser.sQuery, oTokens[5]) || oUtilParser.matchReplace(oUtilParser.sQuery, oTokens[6])) {
				return whereclause();
			}
		}

		return 0;
	}

	public int comparison() {
		String sLogicOp = "";
		String sVal1 = "";
		String sVal2 = "";
		int iLogicOp = 0;
		int iType1 = 0;
		int iType2 = 0;
		if (number() == -1 & string() == -1 & name() == -1) {
			//Parse failure
			return -1;
		}
		iType1 = iTypeIndicator;
		if (iType1 == db_util.DB_DATATYPE_VARCHAR)
			sVal1 = oUtilParser.sValue.substring(1, oUtilParser.sValue.length()-1).replace("''", "'");
		else
			sVal1 = oUtilParser.sValue;
		
		if(logicoperator() == -1)
			return -1;
		sLogicOp = oUtilParser.sValue;
		if (sLogicOp.equals("="))
			iLogicOp = db_util.DB_COMPARE_EQ;
		if (sLogicOp.equals(">="))
			iLogicOp = db_util.DB_COMPARE_EQGT;
		if (sLogicOp.equals("<="))
			iLogicOp = db_util.DB_COMPARE_EQLT;
		if (sLogicOp.equals(">"))
			iLogicOp = db_util.DB_COMPARE_GT;
		if (sLogicOp.equals("<"))
			iLogicOp = db_util.DB_COMPARE_LT;
		
		if (number() != -1 & string() != -1 & name() != -1) {
			//Parse failure
			return -1;
		}

		iType2 = iTypeIndicator;
		if (iType2 == db_util.DB_DATATYPE_VARCHAR)
			sVal2 = oUtilParser.sValue.substring(1, oUtilParser.sValue.length()-1).replace("''", "'");
		else
			sVal2 = oUtilParser.sValue;
		oRes.oConditions.add(new db_where_condition(sVal1, sVal2, iType1, iType2, iLogicOp));

		return 0;
	}

	public int logicoperator() {


		if (!oUtilParser.matchReplaceNoSpace(oUtilParser.sQuery, oTokens[6]) && !oUtilParser.matchReplace(oUtilParser.sQuery, oTokens[7]) && !oUtilParser.matchReplace(oUtilParser.sQuery, oTokens[8]) && !oUtilParser.matchReplace(oUtilParser.sQuery, oTokens[9]) && !oUtilParser.matchReplace(oUtilParser.sQuery, oTokens[10])) {
			//Parse failure
			return -1;
		}
		return 0;
	}

	public int number() {
		
		if (!oUtilParser.matchReplace(oUtilParser.sQuery, oRegex[1]))
			return -1;
		
		iTypeIndicator = db_util.DB_DATATYPE_INT;
		
		return 0;
	}

	public int string() {
		
		if (!oUtilParser.matchReplace(oUtilParser.sQuery, oRegex[2]))
			return -1;

		iTypeIndicator = db_util.DB_DATATYPE_VARCHAR;
		return 0;
	}

	public int name() {
		
		if (!oUtilParser.matchReplace(oUtilParser.sQuery, oRegex[3]))
			return -1;
	
		iTypeIndicator = db_util.DB_DATATYPE_COLUMN;
		return 0;
	}
}
